package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.dao.CmfzUserDao;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class UserServiceImpl implements UserService {

    Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private CmfzUserDao cmfzUserDao;


    @Override
    public Page<CmfzUser> selectAll(String name,Integer page, Integer limit) {
        logger.info("分页方法执行了!!!!!!!");
        //1.创建一个分页对象 封装分页数据
        Page<CmfzUser> userPage = new Page<>(page, limit);
        //2.使用分页查询的方法

        QueryWrapper<CmfzUser> queryWrapper = new QueryWrapper<>();
        //2.将条件写入 条件构造器
        queryWrapper.like("user_telphone",name);
        Page<CmfzUser> cmfzUserPage=null;
        if (name!=null){
            cmfzUserPage = cmfzUserDao.selectPage(userPage, queryWrapper);

        }else {

            cmfzUserPage = cmfzUserDao.selectPage(userPage, null);
        }

        return cmfzUserPage;
    }

    @Override
    public CmfzUser showOne(Integer id) {

        return cmfzUserDao.selectById(id);
    }

    @Override
    public void insert(CmfzUser cmfzUser) {
        try {
            cmfzUserDao.insert(cmfzUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            cmfzUserDao.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(CmfzUser cmfzUser) {
        try {
            cmfzUserDao.update(cmfzUser,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void deleteAll(List<CmfzUser> ids) {
        try {
            cmfzUserDao.deleteBatchIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Integer> selectCountSex() {
        return cmfzUserDao.selectCountSex();
    }

    @Override
    public List<Map> selectPro() {

        return cmfzUserDao.selectPro();
    }


    @Override
    public Map selectByDayCount() {
        Map map = new HashMap();
        map.put("one",cmfzUserDao.selectByDayCount(7,1));
        map.put("two",cmfzUserDao.selectByDayCount(14,7));
        map.put("three",cmfzUserDao.selectByDayCount(21,14));
        return map;
    }

    /**
     * 优化
     * @return
     * @throws InterruptedException
     */
    @Override
    @AddCacheAnnotation
    public Map selectByThread() throws InterruptedException {
        Map map = new HashMap();
//        1.创建计数器  参数为任务数量
        CountDownLatch countDownLatch = new CountDownLatch(3);

//        2.创建子线程执行三个任务
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                注册量查询
                Map map1 = selectByDayCount();
                map.put("count",map1);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                男女人数
                List<Map> maps1 = selectBySexCount();
                map.put("sex",maps1);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                地区分布
                List<Map> maps = selectPro();
                map.put("province",maps);
//                计数器减一  countDown()
                countDownLatch.countDown();
            }
        });

//        3.通过计数器让主线程等待
        countDownLatch.await();

//        4.return
        return map;
    }


    /**
     * 没有优化
     * @return
     */
    @Override
    @AddCacheAnnotation
    public Map selectAllCount() {
        Map map = new HashMap<>();
//        注册量
        Map map1 = selectByDayCount();
//        地区分布
        List<Map> maps = selectPro();
//        男女比例
        List<Map> maps1 = selectBySexCount();
        map.put("sex",maps1);
        map.put("count",map1);
        map.put("province",maps);
        return map;
    }

    @Override
    public List<Map> selectBySexCount() {
        return cmfzUserDao.selectBySexCount();
    }
}
